package com.smartleanx.module.hrm.service.employee;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.module.hrm.controller.admin.employee.growth.EmployeeGrowthRecordRespVO;
import com.smartleanx.module.hrm.controller.admin.employee.growth.EmployeeGrowthRecordSaveReqVO;
import com.smartleanx.module.hrm.intergration.AdminUserServiceProvider;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeGrowthRecordDO;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeGrowthRecordMapper;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.EMPLOYEE_GROWTH_RECORD_NOT_EXISTS;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.EMPLOYEE_NOT_EXISTS;

/**
 * 员工成长轨迹 Service 实现类
 *
 * @author tengcj
 */
@Service
@Validated
public class EmployeeGrowthRecordServiceImpl implements EmployeeGrowthRecordService {

    @Resource
    private EmployeeGrowthRecordMapper employeeGrowthRecordMapper;

    @Resource
    @Lazy
    private EmployeeService employeeService;

    @Resource
    private AdminUserServiceProvider adminUserServiceProvider;

    @Override
    public Long createEmployeeGrowthRecord(EmployeeGrowthRecordSaveReqVO createReqVO) {
        // 插入
        EmployeeGrowthRecordDO employeeGrowthRecord = BeanUtils.toBean(createReqVO, EmployeeGrowthRecordDO.class);
        employeeGrowthRecordMapper.insert(employeeGrowthRecord);
        // 返回
        return employeeGrowthRecord.getId();
    }

    @Override
    public void updateEmployeeGrowthRecord(EmployeeGrowthRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateEmployeeGrowthRecordExists(updateReqVO.getId());
        // 更新
        EmployeeGrowthRecordDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeGrowthRecordDO.class);
        employeeGrowthRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmployeeGrowthRecord(Long id) {
        // 校验存在
        validateEmployeeGrowthRecordExists(id);
        // 删除
        employeeGrowthRecordMapper.deleteById(id);
    }

    private void validateEmployeeGrowthRecordExists(Long id) {
        if (employeeGrowthRecordMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_GROWTH_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeGrowthRecordDO getEmployeeGrowthRecord(Long id) {
        return employeeGrowthRecordMapper.selectById(id);
    }

    @Override
    public List<EmployeeGrowthRecordRespVO> getEmployeeGrowthRecordList(Long employeeId) {
        if (employeeService.getEmployeeById(employeeId) == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        List<EmployeeGrowthRecordDO> recordDOS = employeeGrowthRecordMapper.selectListByEmployeeId(employeeId);
        List<EmployeeGrowthRecordRespVO> list = CollectionUtils.convertList(recordDOS, r -> {
            EmployeeGrowthRecordRespVO respVO = BeanUtils.toBean(r, EmployeeGrowthRecordRespVO.class);
            respVO.setCreateTime(r.getCreateTime());
            respVO.setCreator(r.getCreator());
            return respVO;
        });
        List<EmployeeGrowthRecordRespVO> collect = list.stream().sorted(Comparator.comparing(EmployeeGrowthRecordRespVO::getId).reversed()).collect(Collectors.toList());
        return adminUserServiceProvider.creatorAndUpdaterMapToNickname(collect);
    }

    @Override
    public void deleteByEmployeeId(Long employeeId) {
        employeeGrowthRecordMapper.delete(new LambdaQueryWrapper<EmployeeGrowthRecordDO>(){{
            eq(EmployeeGrowthRecordDO::getEmployeeId,employeeId);
        }});
    }
}
